<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

/**
 * 前台公共库文件
 * 主要定义前台公共函数库
 */

/**
 * 检测验证码
 * @param  integer $id 验证码ID
 * @return boolean     检测结果
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function check_verify($code, $id = 1){
	$verify = new \Think\Verify();
	return $verify->check($code, $id);
}

/**
 * 获取列表总行数
 * @param  string  $category 分类ID
 * @param  integer $status   数据状态
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function get_list_count($category, $status = 1){
    static $count;
    if(!isset($count[$category])){
        $count[$category] = D('Document')->listCount($category, $status);
    }
    return $count[$category];
}

/**
 * 获取段落总数
 * @param  string $id 文档ID
 * @return integer    段落总数
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function get_part_count($id){
    static $count;
    if(!isset($count[$id])){
        $count[$id] = D('Document')->partCount($id);
    }
    return $count[$id];
}

/**
 * 获取导航URL
 * @param  string $url 导航URL
 * @return string      解析或的url
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function get_nav_url($url){
    switch ($url) {
        case 'http://' === substr($url, 0, 7):
        case '#' === substr($url, 0, 1):
            break;
        default:
            $url = U($url);
            break;
    }
    return $url;
}

/**
 * 根据分类id获取广告列表
 */
function get_cate_poster($id){
    if( empty($id) ){
        return false;
    }

    $cate = M('Category')->where(['id'=>$id])->find();
    if( $cate['pid'] != 0 ){
       $cate = M('Category')->where(['id'=>$cate['pid']])->find();
    }
    return  get_cover($cate['icon'],'path');
}
/**
 * 查找会员名称
 */
function getUserMember($id){
    $data = D('UserMember') ->field('nickname') ->find($id);
    return $data['nickname'];
}
/**
 *$ID用户id
*/
function getLogin($id,$field='logo'){
    $data =D('UserMember') ->where(array('id' =>$id)) ->field($field) ->find();
    return $data[$field];
}

/**
 * 判断用户是否为会员
 * @param  [int]  $id [用户id]
 * @return boolean
 */
function is_vip($id) {
    $map['id'] = $id;
    $info = D('UserMember') -> where($map) -> find();
    if (!empty($info)) {
        if ($info['vip_endtime'] < time()) {
            return false;
        } else {
            return true;
        }
    } else {
        return false;
    }
}

//用户是否被举报
function get_pause($id) {
    $map['id'] = $id;
    $info = D('UserMember') -> field('pause') -> where($map) -> find();
    return $info['pause'];
}

//获取课程名称
function getCourse($id){
    $res = D('Course') -> field('title') -> where(array('id'=>$id)) -> find();
    return $res['title'];
}
//获取用户昵称
function getNicknameByCid($cid){
    $res = D('Course') -> alias('c') -> field('u.nickname') -> join('tutor_user_member as u on c.uid=u.id') -> where(array('c.id'=>$cid)) -> find();
    return $res['nickname'];
}

//查询用户教过的学生数
function teached($uid){
    $where['u.id'] = $uid;
    $where['r.at_class'] = 1;

    //查询未上课未退订的订单
    $map['at_class'] = 0;
    $map['status'] = 1;
    $child = D('CourseRecord') -> field('order_id') -> where($map) -> group('order_id') -> select();
    foreach ($child as $v) {
        $not[] = $v['order_id'];
    }
    unset($v);
    if (!empty($not)) {
        $where['r.order_id'] = array('not in',$not);
    }
    $res = D('UserMember')
            -> alias('u')
            -> field('r.*')
            -> join('tutor_course as c on u.id=c.uid')
            -> join('tutor_course_record as r on c.id=r.cid')
            -> where($where)
            -> group('r.order_id')
            -> select();
    return count($res);
}
//是否认证
function Ispassed($uid){
    $map['uid'] = $uid;
    $data = M('Auth') ->field('passed') ->where($map) ->find();
    return $data['passed'];
}
 /*拼接字符串*/
  function getkecheng($str){
      $arr = "";
      foreach ($str as $v){
          $arr.= "<span class='class-span'>{$v}&emsp;</span>";
      }
      return $arr;
  }